<?php
include './functions.php';

if(!isset($_SESSION['username'])){
	exit(json_encode(['error'=>1,'msg'=>'没有登录，不能发布']));
}

$data = $_POST;

if(strlen($data['content'])<10){
	exit(json_encode(['error'=>1,'msg'=>'内容长度不符合要求']));
}

$data['username'] = $_SESSION['username'];

$data['ctime'] = time();

$oldData = file_get_contents('./data/db.txt');
// 把json数据转成数组 
$oldData = json_decode($oldData,true);

$oldData[] = $data;
//转json格式
$oldData = json_encode($oldData);
// 写到数据库文件
file_put_contents('./data/db.txt',$oldData);

exit(json_encode(['error'=>0,'msg'=>'发布成功']));